/**
 * @作者:黎谢之
 * @创建时间：2021/7/7
 * @修改时间：2021/7/10
 */
package com.example.senior_bug.Service;

import com.example.senior_bug.EntityClass.Channel;
import com.example.senior_bug.ToolClass.Result;
import com.example.senior_bug.Repository.CategoryRepository;
import com.example.senior_bug.Repository.ChannelRepository;
import com.example.senior_bug.enm.MsgId;
import com.example.senior_bug.utils.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class CategoryService {
    @Autowired
    private ChannelRepository channelRepository;
    @Autowired
    private CategoryRepository categoryRepository;

    /*用户二级页面传入一级分类名字获取二级分类列表
     * */
    public Result GetCategoriesByChannel(String ChannelName) {
        //判断该一级分类是否存在
        if (channelRepository.existsByName(ChannelName)) {
            Channel channel = channelRepository.findByName(ChannelName);
            return ResultUtils.success(categoryRepository.findByChannelId(channel.getId()));
        } else {
            return ResultUtils.error(MsgId.CHANNEL_NOT_FOUND);
        }
    }

    /*获取二级分类
    * */
    public Result GetCategoryById(int Id) {
        if (categoryRepository.existsById(Id)) {
            return ResultUtils.success(categoryRepository.findById(Id));
        } else {
            return ResultUtils.error(MsgId.CATEGORY_NOT_FOUND);
        }

    }
}
